package logic

import (
	"context"
	"ewaimai/waimairpc/models"
	"ewaimai/waimairpc/pkg"

	"ewaimai/waimairpc/internal/svc"
	"ewaimai/waimairpc/waimairpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealNameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRealNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealNameLogic {
	return &RealNameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RealNameLogic) RealName(in *waimairpc.RealNameRequest) (*waimairpc.RealNameResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var r models.Real
	r.UserId = int(in.UserId)
	if err = r.GetReal(int(in.UserId)); err != nil {
		logx.Errorf("用户不存在")
		return nil, err
	}
	if r.ID > 0 {
		logx.Errorf("用户已存在")
	}
	r.IDCard = in.IDCard
	r.AuthName = in.AuthName
	if !pkg.Auth(in.IDCard, in.AuthName) {
		logx.Errorf("实名认证失败")
	}
	if err = r.CreateRealName(); err != nil {
		logx.Errorf("实名认证失败")
	}
	return &waimairpc.RealNameResponse{
		Id: int64(r.ID),
	}, nil
}
